require(['./config'], () => {
    require(['template','header', 'footer'], (template) => {
      class ShopList {
        constructor () {
            this.getTabs().then(()=>{
                this.sort()
            })
            this.getNextPage()
            this.getIndexPage()
        }
        getTabs(){
          const id = location.search.slice(4)
          return new Promise((resolve,reject)=>{
              $.get(`http://www.xiongmaoyouxuan.com/api/tab/${id}`,resp=>{
                  const{list}=resp.data.items
                if(resp.code===200){
                  $('#goodsPrice').html(
                    template('goodsPriceTemplate', { list:list.slice(1,17) })
                  )
                }
                resolve()
            })
          })
        
        }
        getNextPage(){
          $('#nextPage').on('click',function(){
            const id = location.search.slice(4)
            $.get(`http://www.xiongmaoyouxuan.com/api/tab/${id}/feeds?start=20&sort=0`,(resp)=>{
            const{list}=resp.data
              if(resp.code===200){
                $('#goodsPrice').html(
                  template('goodsPriceTemplate', { list:list.slice(1,17) })
                )
              }
            })
          })
        }
        getIndexPage(){
          $('#indexPage').on('click',()=>{
            this.getTabs()
          })
          
        }
        sort(){
          let array = []
          let prev
          let after
          $('.g-list').each(function (index) { 
            //$(this)是16个g-list
            array[index] = $(this)
            $('.sort a').each(function(index){
                $(this).on('click',function(){
                    if(index==0){
                        //升序
                        for(let i=0;i<array.length-1;i++){
                          for (let j=0;j<array.length-i-1;j++){
                              prev = parseFloat(array[j].find('.g-price').children('strong').html()).toFixed(2);
                              after = parseFloat(array[j+1].find('.g-price').children('strong').html()).toFixed(2);
                              if(parseFloat(prev) > parseFloat(after)){
                                  let temp = array[j+1];
                                  array[j+1]=array[j];
                                  array[j]=temp;
                              }
                          }
                      }
      
                    }else if(index==1){
                      for(let i=0;i<array.length;i++){
                        for (let j=0;j<array.length-i-1;j++){
                            prev = array[j].find('.g-price').children('strong').html();
                            after = array[j+1].find('.g-price').children('strong').html();
                            if(parseFloat(prev) < parseFloat(after)){
                                console.log(prev,after);
                                let temp = array[j+1];
                                array[j+1]=array[j];
                                array[j]=temp;
                            }
                        }
                      }
    
                    }else if(index==2){
                      for(let i=0;i<array.length;i++){
                        for (let j=0;j<array.length-i-1;j++){
                            prev = array[j].find('.gp-num').children('strong').html();
                            after = array[j+1].find('.gp-num').children('strong').html();
                            if(parseFloat(prev) < parseFloat(after)){
                                let temp = array[j+1];
                                array[j+1]=array[j];
                                array[j]=temp;
                            }
                        }
                      }
                    }
                    $('#goodsPrice').empty()
                    for(let i=0;i<array.length;i++){
                      $('#goodsPrice').append(array[i])
                    }
      
                })
            })
            
          })
         
        }
      
      }
      new ShopList()
    })
  })
  